﻿-- Libraries
local L = AceLibrary("AceLocale-2.2"):new("Nier")
local waterfall = AceLibrary:HasInstance("Waterfall-1.0") and AceLibrary("Waterfall-1.0")

BINDING_HEADER_NIER = "Nier"
BINDING_NAME_NIER_TOGGLE = L["Toggle gears"]
BINDING_NAME_NIER_EQUIP = L["Equip gears"]
BINDING_NAME_NIER_UNEQUIP = L["Unequip gears"]

local check_libraries
do
	local global_libs = {
		"AceLibrary",
		"LibStub",
		-- "DoesNotExist-Global",
	}

	local ace2_libs = {
		"AceLocale-2.2",
		"Dewdrop-2.0",
		"Waterfall-1.0",
		-- "DoesNotExist-Ace2",
	}

	local libstub_libs = {
		"LibGratuity-3.0",
		-- "DoesNotExist-LibStub",
	}

	function check_libraries ()
		local missing = {}

		for _, name in ipairs(global_libs) do
			if not _G[name] then
				table.insert(missing, name)
			end
		end

		for _, name in ipairs(ace2_libs) do
			if not AceLibrary:HasInstance(name) then
				table.insert(missing, name)
			end
		end

		for _, name in ipairs(libstub_libs) do
			if not LibStub:GetLibrary(name, true) then
				table.insert(missing, name)
			end
		end

		if #missing > 0 then
			local message = ("Nier was unable to find the following libraries:\n%s"):format(table.concat(missing, ", "))

			StaticPopupDialogs["NIER_MISSING_LIBS"] = {
				text = message,
				button1 = "Okay",
				timeout = 0,
				whileDead = 1,
			}

			StaticPopup_Show("NIER_MISSING_LIBS")
		end
	end
end

-- Initialization
Nier = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceDB-2.0", "AceDebug-2.0", "AceConsole-2.0", "AceModuleCore-2.0", "FuBarPlugin-2.0")
Nier:SetModuleMixins("AceDebug-2.0", "AceEvent-2.0")
Nier:RegisterDB("NierDB")
Nier.debugFrame = ChatFrame1
Nier.check_libraries = check_libraries
Nier.modules = {}

-- Option table
Nier.options = {
	type = "group",
	handler = Nier,
	args = {
	},
}

-- DB default
Nier.defaults = {
	enableAddon = true,
	debug = false,
}

Nier.modulePrototype.core = Nier
function Nier.modulePrototype:OnInitialize()
	if not self.db then
		self.core:RegisterDefaults(self.name, "profile", self.defaultDB or {})
		self.db = self.core:AcquireDBNamespace(self.name)
	end
	if self.options then
		for k, v in pairs(self.options) do
			self.core.options.args[k] = v
		end
	end
	self.debugFrame = self.core.debugFrame
	self.debugging = self.core.db.profile.debug
	self:Debug("OnInitialize")
end

function Nier.modulePrototype:OnEnable()
end

function Nier:OnInitialize()
	self:RegisterDefaults("profile", Nier.defaults)
	self:RegisterChatCommand({"/nier"}, self.options)

	self.debugging = self.db.profile.debug

	if not FuBar then
		self.OnMenuRequest.args.hide.guiName = L["Hide minimap icon"]
		self.OnMenuRequest.args.hide.desc = L["Hide minimap icon"]
	end
end

function Nier:OnEnable()
	check_libraries()
	for name, module in self:IterateModules() do
		if module and type(module.OnEnable) == "function" then
			module:OnEnable()
		end
	end
end

function Nier:OnProfileEnable()
	self.debugging = self.db.profile.debug
	self:Debug("Loaded profile", "(", self:GetProfile(),")")
end

function Nier:OnDebugEnable()
	self.db.profile.debug = true
	for name, module in self:IterateModules() do
		module.debugging = true
	end
end

function Nier:OnDebugDisable()
	self.db.profile.debug = false
	for name, module in self:IterateModules() do
		module.debugging = false
	end
end
